package com.czy.common.exception;

import com.czy.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局异常处理器
 * @author czy
 * @date 2021/3/29
 */
@RestControllerAdvice
//@ControllerAdvice //不返回json格式数据
public class GlobalExceptionHandler {
    private static final Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public Result businessException(CustomException e) {
        if (e.getCode()==null) {
            return Result.error(e.getMessage());
        }
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * json解析异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result jsonParseException(HttpMessageNotReadableException e){
        log.error(e.getMessage(), e);
        return Result.error(500,"请求参数解析异常,请检查参数");
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public Result validatedBindException(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return Result.error(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return Result.error(message);
    }

    /**
     * 请求方法不支持
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
        log.error(e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 以上异常都不满足则在此处处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result globalException(Exception e){
        e.printStackTrace();
        return Result.error(e.getMessage());
    }

    /**
     * 返回错误页面
     * @param e
     * @return
     */
    @ExceptionHandler(AuthException.class)
    public ModelAndView myErrorHandler(AuthException e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("code", e.getCode());
        modelAndView.addObject("msg", e.getMessage());
        return modelAndView;
    }
}
